using System;
using System.IO;
using System.Linq;
using SkiaSharp;
using UMC.Web;
using UMC.Web.UI;

namespace UMC.ITME.Activities
{

    [Apiumc("ITME", "BgSrc", Auth = WebAuthType.Admin, Desc = "登录背景图片")]
    class SiteBgSrcActivity : UMC.Web.WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var cfg = UMC.Data.Reflection.Configuration("BgSrc");
            var name = this.AsyncDialog("Name", g =>
            {
                var title = UITitle.Create();

                title.Title = "登录背景";
                var ui = UISection.Create(title);
                var webr = UMC.Data.WebResource.Instance();
                title.Right(new UIEventText("添加").Click(new UIClick(request, g, "New")));

                foreach (var d in cfg.Providers)
                {
                    var start = d["start"];
                    var end = d["end"];
                    var cell = new UIImageTextValue(webr.ImageResolve(request.Url, d.Name, "1", 4, d["time"]), String.Empty, $"在{start}到{end}展示");
                    cell.Click(new UIClick(request, g, d.Name));

                    cell.Style.Name("image-width", 72);
                    cell.Style.Name("image-radius", 10);
                    ui.Delete(cell, new UIEventText("删除").Click(new UIClick(request, g, d.Name, "Model", "Del")));
                }
                if (cfg.Providers.Count == 0)
                {

                    var desc = new UIDesc("未设置登录背景，请添加");
                    desc.Put("icon", "\uf016").Format("desc", "{icon}\n{desc}");
                    desc.Style.Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60));
                    ui.Add(desc);
                }
                ui.SendTo(this.Context, $"{request.Model}.{request.Command}");
            });
            switch (this.AsyncDialog("Model", "New"))
            {
                case "Del":

                    cfg.Remove(name);
                    UMC.Data.Reflection.Configuration("BgSrc", cfg);
                    this.Context.Send(true);
                    break;

            }
            var provider = cfg[name];// ?? UMC.Data.Provider.Create(name, "BgSrc");
            if (provider == null)
            {
                provider = UMC.Data.Provider.Create(Utility.TimeSpan().ToString(), "BgSrc");
                request.Arguments["Name"] = provider.Name;// Utility.TimeSpan().ToString();
            } 
            var setting = this.AsyncDialog("Settings", r =>
            {
                var form = new UIFormDialog() { Title = "背景图片" };
                form.AddNumber("展示开始时段", "start", provider["start"] ?? "0").PlaceHolder("24小时制时段");
                form.AddNumber("展示结束时段", "end", provider["end"] ?? "23").PlaceHolder("24小时制时段");
                form.AddFile("背景图片", "Image", String.Empty);


                form.Submit("确认", $"{request.Model}.{request.Command}");
                return form;
            });
            
            var media_id = setting["Image"];
            this.AsyncDialog("Image", "System", "Image", new WebMeta().Put("id",  provider.Name, "media_id", media_id));
            
            provider.Attributes["start"] = setting["start"];
            provider.Attributes["end"] = setting["end"];
            provider.Attributes["time"] = Utility.TimeSpan().ToString();

            cfg.Add(provider);
            UMC.Data.Reflection.Configuration("BgSrc", cfg);
            this.Context.Send(true);

        }
    }
}